# import time
# import torch
# from mmdet.apis import init_detector, inference_detector
#
# # 加载模型
# model = init_detector('/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/work_dirs/ablation/config/diffusiondet_r50_lamfpn8_epoch_microalgeaOri_0lcm2_1adem2_0ddim4_0distill4_memeryOptim/20250412_224919/vis_data/config.py',
#                       '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/work_dirs/ablation/config/diffusiondet_r50_lamfpn8_epoch_microalgeaOri_0lcm2_1adem2_0ddim4_0distill4_memeryOptim/best_coco_bbox_mAP_epoch_47.pth', device='cuda:0')
# # model = init_detector('/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/work_dirs/diffusiondet_r50_fpn_epoch_microalgea_lcm_adem.py/diffusiondet_r50_lamfpn2_epoch_microalgeaOri_lcm_adem.py',
# #                       '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/SOTA/work_dirs/diffusiondet_r50_lamfpn2_epoch_microalgea_lcm_adem.py/epoch_100.pth', device='cuda:0')
# # model = init_detector('/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/SOTA/work_dirs/diffusiondet_r50_fpn_epoch_microalgeaOri/diffusiondet_r50_fpn_epoch_microalgeaOri.py',
# #                       '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/SOTA/work_dirs/diffusiondet_r50_fpn_epoch_microalgeaOri/epoch_100.pth', device='cuda:0')
#
# # 预热模型
# img = '/media/ross/8TB/project/lsh/dataset/microAlgea/mmdetection_format/test/0e6f88b5dca4cdc699902164229b330b5af255888811b273354bcb46b0d1d857_jpeg.rf.f52e644be91d2eea6f3da06d99f8119d.jpg'
# for _ in range(10):
#     _ = inference_detector(model, img)
#
# # 测速
# iterations = 100
# start_time = time.time()
# for _ in range(iterations):
#     with torch.no_grad():
#         _ = inference_detector(model, img)
# end_time = time.time()
#
# # 计算 FPS
# fps = iterations / (end_time - start_time)
# print(f"FPS: {fps:.2f}")

import time
import torch
import cv2
import numpy as np
from mmdet.apis import init_detector, inference_detector


def test_fps(model, img_path, resolution, iterations=100):
    """测试特定分辨率下的FPS"""
    # 读取图像并调整分辨率
    img = cv2.imread(img_path)
    if img is None:
        print(f"无法读取图像: {img_path}")
        return 0

    img_resized = cv2.resize(img, (resolution[0], resolution[1]))

    # 预热模型
    for _ in range(10):
        _ = inference_detector(model, img_resized)

    # 测速
    start_time = time.time()
    for _ in range(iterations):
        with torch.no_grad():
            _ = inference_detector(model, img_resized)
    end_time = time.time()

    # 计算 FPS
    fps = iterations / (end_time - start_time)
    return fps


# 主程序
if __name__ == "__main__":
    # 加载模型
    config_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet_algae6/algae6/SOTA/work_dirs/algaediff/diffusiondet_0fold_r50_lamfpn8_epoch_algae6_0lcm2_1adem2_1ddim4_0distill4_memeryOptim/20250414_095235/vis_data/config.py'
    checkpoint_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet_algae6/algae6/SOTA/work_dirs/algaediff/diffusiondet_0fold_r50_lamfpn8_epoch_algae6_0lcm2_1adem2_1ddim4_0distill4_memeryOptim/best_coco_bbox_mAP_epoch_42.pth'
    # config_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet_algae6/algae6/SOTA/work_dirs/faster-rcnn/faster-rcnn_0fold_r50_fpn_1x_algae6/20250402_230056/vis_data/config.py'
    # checkpoint_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet_algae6/algae6/SOTA/work_dirs/faster-rcnn/faster-rcnn_0fold_r50_fpn_1x_algae6/epoch_25.pth'
    # config_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/SOTA/work_dirs/faster-rcnn_r50_fpn_1x_microalgaeOri/20250411_134029/vis_data/config.py'
    # checkpoint_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/SOTA/work_dirs/faster-rcnn_r50_fpn_1x_microalgaeOri/epoch_20.pth'
    # config_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/work_dirs/ablation/config/diffusiondet_r50_lamfpn8_epoch_microalgeaOri_0lcm2_1adem2_0ddim4_0distill4_memeryOptim/20250412_224919/vis_data/config.py'
    # checkpoint_file = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/work_dirs/ablation/config/diffusiondet_r50_lamfpn8_epoch_microalgeaOri_0lcm2_1adem2_0ddim4_0distill4_memeryOptim/best_coco_bbox_mAP_epoch_47.pth'


    model = init_detector(config_file, checkpoint_file, device='cuda:0')

    # 测试图像路径
    img_path = '/media/ross/8TB/project/lsh/dataset/microAlgea/mmdetection_format/test/0e6f88b5dca4cdc699902164229b330b5af255888811b273354bcb46b0d1d857_jpeg.rf.f52e644be91d2eea6f3da06d99f8119d.jpg'

    # 定义要测试的分辨率
    resolutions = [
        (320, 240),  # 低分辨率
        (640, 480),  # 中分辨率
        (1280, 720),  # 高分辨率 (HD)
        (1920, 1080)  # 高分辨率 (Full HD)
    ]

    # 测试每种分辨率的FPS
    print("分辨率测试结果:")
    print("-" * 40)
    print(f"{'分辨率':<15} | {'FPS':<10} | {'每帧耗时(ms)':<15}")
    print("-" * 40)

    for resolution in resolutions:
        fps = test_fps(model, img_path, resolution)
        ms_per_frame = 1000 / fps
        print(f"{resolution[0]}x{resolution[1]:<8} | {fps:.2f}     | {ms_per_frame:.2f}")

    print("-" * 40)
    print("注: 测试环境为单GPU, 每个分辨率测试100次迭代取平均值")
